package id.net.iconpln.bb.client.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import id.net.iconpln.bb.client.service.ApprovalService;
import id.net.iconpln.bb.client.service.ApprovalServiceAsync;
import id.net.iconpln.bb.client.service.BatubaraService;
import id.net.iconpln.bb.client.service.BatubaraServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CellSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbRakorPusat extends LayoutContainer {
//	private final ApprovalServiceAsync appservice = GWT.create(ApprovalService.class);
	private final BatubaraServiceAsync service = GWT.create(BatubaraService.class);
	private Grid gridRakor,gridPltu, gridPemasok;
	private EditorGrid gridPltuList;
	private ListStore storeRakor, storePltu, storePemasok, storePltuList;
	private Button btnRefresh, btaddRakor, btupdateRakor, btdeleteRakor, btaddPltu,btremovePltu, btsavePltu;
	private TextField txtnorakor,txtagenda,txttempat,txtalamat,txtket;
	private DateField txttgl;
	private FormPanel formPanelRakor;
	private ToolBar toolbarPemasok, toolbarPltu, toolbarRakor;
	private FormData formData;
	private Window windowRakor, windowPltu;
	private Map mapParam;
	
	List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
	
	@Override
	protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos); 
	    storeRakor = new ListStore(); 
	    storePltu = new ListStore(); 
	    storePemasok = new ListStore();
	    storePltuList = new ListStore();
	    toolbarPemasok = new ToolBar(); 
	    toolbarPltu = new ToolBar();
	    toolbarRakor = new ToolBar();
	    setLayout(new FitLayout());  
	    
	    ContentPanel cp = new ContentPanel();  
	    cp.setHeaderVisible(false); 
	    cp.setFrame(true); 
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL));
	    
//	    gridPltu = createGridkit();
//	    gridPemasok = createGridPemasok();
	    gridRakor = createGridRakor();
	    
	    
	    btaddPltu = new Button("New", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  if (!(gridRakor.getSelectionModel().getSelectedItem() == null)){
		    		  getdataRakorOut(gridRakor.getSelectionModel().getSelectedItem().get("no_rakor").toString());
		    	  }
		      }  
	    });  
	    btaddPltu.setIcon(IconHelper.createStyle("icon-reza-db-add"));
	    
	    btremovePltu = new Button("Remove", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  	
		      }  
	    });  
	    btremovePltu.setIcon(IconHelper.createStyle("icon-reza-db-delete"));
	    
	    createGridkit();
	    
	    ContentPanel cpPltu = new ContentPanel();
	    cpPltu.setHeaderVisible(false);
	    cpPltu.setLayout(new FitLayout());
	    cpPltu.add(gridPltu);
	    toolbarPltu.add(new LabelToolItem("<b> Daftar Peserta PLTU"));
	    toolbarPltu.add(new SeparatorToolItem());
	    toolbarPltu.add(btaddPltu);
	    toolbarPltu.add(new SeparatorToolItem());
	    toolbarPltu.add(btremovePltu);
	    cpPltu.setTopComponent(toolbarPltu);
	    
	    createGridPemasok();
	    
	    ContentPanel cpPemasok = new ContentPanel();
	    cpPemasok.setHeaderVisible(false);
	    cpPemasok.setLayout(new FitLayout());
	    cpPemasok.add(gridPemasok);
	    toolbarPemasok.add(new LabelToolItem("<b> Daftar Peserta Pemasok "));
	    cpPemasok.setTopComponent(toolbarPemasok);
	    
	    
	    btaddRakor = new Button("New", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  windowRakor.show();
		      }  
	    });  
//	    btaddRakor.setIcon(IconHelper.createStyle("icon-add"));	
	    btaddRakor.setIcon(IconHelper.createStyle("icon-reza-db-add"));
	    
	    
	    btupdateRakor = new Button("Edit", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  	
		      }  
	    });  
	    btupdateRakor.setIcon(IconHelper.createStyle("icon-reza-db-edit"));	
	    
	    btdeleteRakor = new Button("Delete", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  	
		      }  
	    });  
	    btdeleteRakor.setIcon(IconHelper.createStyle("icon-reza-db-delete"));
	    
	    ContentPanel cpRakor = new ContentPanel();
	    cpRakor.setHeaderVisible(false);
	    cpRakor.setLayout(new FitLayout());
	    cpRakor.add(gridRakor);
	    toolbarRakor.add(new LabelToolItem("<b> Daftar Rakor"));
	    toolbarRakor.add(new SeparatorToolItem());
	    toolbarRakor.add(btaddRakor);
	    toolbarRakor.add(new SeparatorToolItem());
	    toolbarRakor.add(btupdateRakor);
	    toolbarRakor.add(new SeparatorToolItem());
	    toolbarRakor.add(btdeleteRakor);
	    cpRakor.setTopComponent(toolbarRakor);
	    
	    	
	    
	    
	    ContentPanel panel = new ContentPanel();
	    panel.setHeaderVisible(false);
	    panel.setLayout(new RowLayout(Orientation.VERTICAL));
	    panel.add(cpPltu, new RowData(1, .5));
	    panel.add(cpPemasok, new RowData(1, .5));
	    
	    ContentPanel delimeter = new ContentPanel();
	    delimeter.setHeaderVisible(false);
	    delimeter.setWidth(10);
	    
	    cp.add(cpRakor, new RowData(.5, 1));
	    cp.add(delimeter, new FitData(5));
	    cp.add(panel, new RowData(.5, 1));      
	    
	    add(cp, new FitData(5)); 
	    
	    createForm();
	}
	
	 private void createForm() {  
		    final FormPanel panel = new FormPanel();  
		    panel.setHeaderVisible(false);
		    
		    FieldSet fieldSet2 = new FieldSet();  
		    fieldSet2.setHeading("Undangan Rapat Koordinasi");  
		    fieldSet2.setCollapsible(false);

		    FormLayout layout2 = new FormLayout();  
		    layout2.setLabelWidth(150);  
		    fieldSet2.setLayout(layout2);
		    
		    final TextField<String> txtnorakor = new TextField<String>();
		    txtnorakor.setName("no_rakor");
		    txtnorakor.setFieldLabel("No Rakor");
		    txtnorakor.setEnabled(false);
		    fieldSet2.add(txtnorakor, formData); 
		    
		    final TextArea txtagenda = new TextArea();
		    txtagenda.setName("agenda");
		    txtagenda.setFieldLabel("Agenda");  
		    fieldSet2.add(txtagenda, formData); 
		    
		    final TextField<String> txttempat = new TextField<String>();
		    txttempat.setName("tempat");
		    txttempat.setFieldLabel("Tempat");  
		    fieldSet2.add(txttempat, formData);  
		  
		    final TextField<String> txtalamat = new TextField<String>();
		    txtalamat.setName("alamat");
		    txtalamat.setFieldLabel("alamat");  
		    fieldSet2.add(txtalamat, formData);

		    txttgl = new DateField();
		    txttgl.setPropertyEditor(new DateTimePropertyEditor("dd/MM/yyyy"));
		    txttgl.setFieldLabel("Tanggal");
		    fieldSet2.add(txttgl, formData);
		    
		    final TextArea txtket = new TextArea();
		    txtket.setName("keterangan");
		    txtket.setFieldLabel("Keterangan");  
		    fieldSet2.add(txtket, formData);
		    
		    panel.add(fieldSet2);
		    
		    ContentPanel p = new ContentPanel();
		    p.setHeaderVisible(false);	    
		    p.setButtonAlign(HorizontalAlignment.RIGHT);  
		    p.addButton(new Button("Submit", new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {
		    	  	String session = Registry.get("isModule");
//		    	  	String session = Registry.get("isModule");
//		    	  	String session = Registry.get("isModule");
//		    	  	String session = Registry.get("isModule");
		    	  	mapParam = new FastMap();
		    		mapParam.put("norakor", txtnorakor.getValue());	    		
		    		mapParam.put("agenda", txtagenda.getValue());
		    		mapParam.put("tempat", txttempat.getValue());
		    		mapParam.put("alamat", txtalamat.getValue());
		    		mapParam.put("tanggal", txttgl.getRawValue());
		    		mapParam.put("ket", txtket.getValue());
		    		mapParam.put("createby", session);
		    		mapParam.put("app_access", "WEB");
		    		if (txtnorakor.getRawValue().equals("")){
		    			mapParam.put("crud", "CREATE");
		    		}else{
		    			mapParam.put("crud", "UPDATE");
		    		}
		    		service.insertBbPengendalian("RAKOR", mapParam,new AsyncCallback<List<Map>>() {
		    			public void onFailure(Throwable caught) {
		    				GWT.log(caught.getMessage(), null);
		    			}

		    			public void onSuccess(List<Map> result) {
		    				gridRakor.mask("load Data ....","x-mask-loading");
		    				storeRakor.removeAll();
		    				getdataRakor();
		    				gridRakor.unmask();
		    				panel.clear();
		    			}
		    		});
		    		gridRakor.getStore().commitChanges();
			        Info.display("Notifikasi: ", "Data Berhasil Di Simpan");
		      }  
		    })); 
		    
		    p.addButton(new Button("Delete", new SelectionListener<ButtonEvent>() {  
			      @Override  
			      public void componentSelected(ButtonEvent ce) {  
			    	  	final Map mapParam = new FastMap();
			    	  	String session = Registry.get("isModule");
			    		mapParam.put("norakor", txtnorakor.getValue());	    		
			    		mapParam.put("agenda", txtagenda.getValue());
			    		mapParam.put("tempat", txttempat.getValue());
			    		mapParam.put("alamat", txtalamat.getValue());
			    		mapParam.put("tanggal", txttgl.getRawValue());
			    		mapParam.put("ket", txtket.getValue());
			    		mapParam.put("createby", session);
			    		mapParam.put("app_access", "WEB");
			    		mapParam.put("crud", "DELETE");
			    	  
			    		service.insertBbPengendalian("RAKOR", mapParam,new AsyncCallback<List<Map>>() {
			    			public void onFailure(Throwable caught) {
			    				GWT.log(caught.getMessage(), null);
			    			}

			    			public void onSuccess(List<Map> result) {
			    				gridRakor.mask("load Data ....","x-mask-loading");
			    				storeRakor.removeAll();
			    				getdataRakor();
			    				gridRakor.unmask();
			    				panel.clear();
			    			}
			    		});
			    		gridRakor.getStore().commitChanges();
				        Info.display("Notifikasi: ", "Data Berhasil Di Hapus");
				    }  
			    }));  
		    
		    p.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
			      @Override  
			      public void componentSelected(ButtonEvent ce) {  
//			    	   txtnorakor.clear();
//			    	   txtagenda.clear();
//			    	   txtalamat.clear();
//			    	   txttempat.clear();
//			    	   txttgl_kontrak.clear();
//			    	   txtket.clear();
			    	   panel.clear();
				    }  
			    }));  

		    panel.add(p);
		    windowRakor = new Window();  
		    windowRakor.setHeading("New Rakor");
		    windowRakor.setWidth(400);
		    windowRakor.add(panel);
		      
		  }
	
	 private Grid createGridRakor() {  
		   
			final List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); 
		    
			storeRakor = new ListStore();
			getdataRakor();

			RowNumberer id = new RowNumberer();  
		    id.setWidth(30);
		    
		    ColumnConfig idpemasok = new ColumnConfig("no_rakor", "No Rakor", 150);
		    idpemasok.setAlignment(HorizontalAlignment.LEFT);
		    
		    ColumnConfig agenda = new ColumnConfig("agenda", "Agenda", 120);
		    agenda.setAlignment(HorizontalAlignment.LEFT);
		    
		    ColumnConfig tgl = new ColumnConfig("tanggal", "Tanggal", 100);
		    tgl.setAlignment(HorizontalAlignment.LEFT);
		    
		    ColumnConfig keterangan = new ColumnConfig("keterangan", "Keterangan", 130);
		    keterangan.setAlignment(HorizontalAlignment.LEFT);
		    
		    ColumnConfig tempat = new ColumnConfig("tempat", "Tempat", 130);
		    tempat.setAlignment(HorizontalAlignment.LEFT);
		    
		    ColumnConfig alamat = new ColumnConfig("alamat", "Alamat", 130);
		    alamat.setAlignment(HorizontalAlignment.LEFT);
		    
		    columns.add(idpemasok);
		    columns.add(tgl);
		    columns.add(tempat);
		    columns.add(alamat);
		    columns.add(agenda);
		    columns.add(keterangan);
		    
		   
		    ColumnModel cm = new ColumnModel(columns);
		    
		    gridRakor = new Grid(storeRakor, cm);
		    
		    return gridRakor;  
		  }
	
	 private void getdataRakor() {
		 mapParam = new FastMap();
		 service.getListPengendalianBb("RAKOR",mapParam, new AsyncCallback<List<Map>>() {
	  			public void onFailure(Throwable caught) {
	  				GWT.log(caught.getMessage(), null);
	  			}

	  			public void onSuccess(List<Map> result) {	
	  				storeRakor.removeAll();
	  				storeRakor.add(ClientModule.setListBaseModelFromListMap(result));
	  			}
	  		});
	}
	 
	 private void createGridPemasok() {      
			
		 	storePemasok = new ListStore();			
			RowNumberer rn = new RowNumberer();  
		    rn.setWidth(30);
		    columns.add(rn);
		    
		    ColumnConfig pemasok = new ColumnConfig("pemasok", "Pemasok", 150);
		    pemasok.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(pemasok);
		    
		    ColumnConfig alamat = new ColumnConfig("alamat", "Alamat", 150);
		    alamat.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(alamat);
		    
		    ColumnConfig pic_presdir = new ColumnConfig("pic_presdir", "PIC Pres Dir", 150);
		    pic_presdir.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(pic_presdir);
		    
		    ColumnConfig pic_dir_marketing = new ColumnConfig("pic_dir_marketing", "PIC Dir Marketing", 150);
		    pic_dir_marketing.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(pic_dir_marketing);

		    ColumnConfig pic_man_opr = new ColumnConfig("pic_man_opr", "PIC Mgr Operasi", 150);
		    pic_man_opr.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(pic_man_opr);
		    
		    ColumnConfig status_pemasok = new ColumnConfig("status_pemasok", "Status Pemasok", 150);
		    status_pemasok.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(status_pemasok);
		    
		    ColumnConfig nama_tambang = new ColumnConfig("nama_tambang", "Nama Tambang", 150);
		    nama_tambang.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(nama_tambang);
		    
		    ColumnConfig produksi = new ColumnConfig("produksi", "Produksi", 150);
		    produksi.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(produksi);
		    
		    ColumnConfig loading_port = new ColumnConfig("loading_port", "Loading Port", 150);
		    loading_port.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(loading_port);
		    
		    ColumnConfig fasilitas_loading = new ColumnConfig("fasilitas_loading", "Fasilitas Loading", 150);
		    fasilitas_loading.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(fasilitas_loading);
		    
		    ColumnConfig jarak_hauling = new ColumnConfig("jarak_hauling", "Jarak Hauling", 150);
		    jarak_hauling.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(jarak_hauling);
		    
		    ColumnConfig flag_blacklist = new ColumnConfig("flag_blacklist", "Flag Blacklist", 150);
		    flag_blacklist.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(flag_blacklist);
		    
		    ColumnConfig status_aktif = new ColumnConfig("status_aktif", "Status Aktif", 150);
		    status_aktif.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(status_aktif);
		    
		    ColumnConfig keterangan = new ColumnConfig("keterangan", "Keterangan", 150);
		    keterangan.setAlignment(HorizontalAlignment.LEFT);
			columns.add(keterangan);
		  
		    final ColumnModel cm = new ColumnModel(columns);	    
		    gridPemasok = new Grid(storePemasok, cm);
		    gridPemasok.setBorders(true);  	    
//		    return gridPemasok;  
		}
	 
	 	private void getdataPemasok(String id_pltu) {
		Map map = new FastMap();
		map.put("id_pltu", id_pltu);
		service.getListpemasok_rakor("PEMASOK_BY_PLTU", map , new AsyncCallback<List<Map>>() {
			public void onFailure(Throwable caught) {
				GWT.log(caught.getMessage(), null);
			}

			public void onSuccess(List<Map> result) {	
				storePemasok.removeAll();
				storePemasok.add(ClientModule.setListBaseModelFromListMap(result));
			}
		});
	}
	 
	 private void createGridkit() { 
			final List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
			final List<ColumnConfig> columnsList = new ArrayList<ColumnConfig>();
			storePltu = new ListStore();
//			getdataKit("");//di isi variable norakor 
			
			RowNumberer id = new RowNumberer();  
		    id.setWidth(30);
		    
		    ColumnConfig nmkit = new ColumnConfig("nama_pembangkit", "Nama Pembangkit", 250);
		    nmkit.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(nmkit);
		    
		    ColumnConfig nmpic = new ColumnConfig("nama_pic", "Nama PIC", 120);
		    nmpic.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(nmpic);
		    
		    ColumnConfig alamat = new ColumnConfig("alamat", "Alamat", 100);
		    alamat.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(alamat);
		    
		    ColumnConfig pemilik = new ColumnConfig("kepemilikan", "Pemilik", 100);
		    pemilik.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(pemilik);
		    
		    ColumnConfig wilayah = new ColumnConfig("wilayah", "Wilayah", 130);
		    wilayah.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(wilayah);
		    
		    ColumnConfig jeniskit = new ColumnConfig("jenis_pembangkit", "Jenis Pembangkit", 130);
		    jeniskit.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(jeniskit);
		    
		    ColumnConfig tlp = new ColumnConfig("telp", "Telp", 130);
		    tlp.setAlignment(HorizontalAlignment.LEFT);
		    
		    ColumnConfig daya = new ColumnConfig("daya_terpasang", "Daya Terpasang", 130);
		    daya.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(daya);
		    
		    ColumnConfig sfc = new ColumnConfig("sfc", "SFC", 130);
		    sfc.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(sfc);
		    
		    ColumnConfig capacity_F = new ColumnConfig("capacity_factor", "Capacity Factor", 130);
		    capacity_F.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(capacity_F);
		    
		    ColumnConfig hear_rate = new ColumnConfig("heat_rate", "Heat Rate", 130);
		    hear_rate.setAlignment(HorizontalAlignment.LEFT);
		    columns.add(hear_rate); 
		    
		    final ColumnModel cm = new ColumnModel(columns);
		    gridPltu = new Grid(storePltu, cm);
		   
		    CheckColumnConfig checkColumn = new CheckColumnConfig("select", "Select", 55); 
		    CheckBox cb = new CheckBox();
		    cb.setValue(true);
		    CellEditor checkBoxEditor = new CellEditor(cb);  
		    checkColumn.setEditor(checkBoxEditor);  
		    columnsList.add(checkColumn);
		    columnsList.add(nmkit);
		    final ColumnModel cmList = new ColumnModel(columnsList);
		    gridPltuList = new EditorGrid(storePltuList, cmList);
		    gridPltuList.setBorders(true); 
		    gridPltuList.setHeight(280);
		    
		    btsavePltu = new Button("Save");
		    btsavePltu.addSelectionListener(new SelectionListener<ButtonEvent>() {
				
				@Override
				public void componentSelected(ButtonEvent ce) {
					// TODO Auto-generated method stub
					for (int x=0 ; x < gridPltuList.getStore().getModels().size(); x++){
		        		String v = "";
		        		try{
		        			v = gridPltuList.getStore().getAt(x).get("selectPemasok").toString();
		        		}catch (Exception e) {
							v = "";
						}
		        		if (v.equals("true")){
		        			setApprovalKit(gridPltuList.getStore().getAt(x).get("id_pltu").toString() ,
		        					gridRakor.getSelectionModel().getSelectedItem().get("no_rakor").toString(), "CREATE");
		        		}
		        		if ( x == gridPltuList.getStore().getModels().size() - 1){
		        			getdataPemasok(gridPltuList.getSelectionModel().getSelectedItem().get("id_pltu").toString());        			
		        		}
		        	}
					
					windowPltu.hide();
				}
			});
		    ToolBar toolbarListPemasok = new ToolBar();
		    toolbarListPemasok.add(btsavePltu);
		    
		    storePltuList = new ListStore();
		    gridPltuList = new EditorGrid(storePltuList, cmList);
		    gridPltuList.setBorders(true); 
		    gridPltuList.setHeight(280);
		    
		    windowPltu = new Window();  
		    windowPltu.setHeading("List PLTU");
		    windowPltu.setSize(400, 300);
		    windowPltu.add(gridPltuList);
		    windowPltu.setTopComponent(toolbarListPemasok);
	    
//	        return gridPltu;   
		}
	 
	 private void getdataRakorOut(String no_rakor) {
			Map map = new FastMap();
			map.put("no_rakor", no_rakor);
			System.out.println("no rakor ui: " + no_rakor);
			service.getListpltu_rakor("RAKOR_BY_PLTU_OUT", map , new AsyncCallback<List<Map>>() {
				public void onFailure(Throwable caught) {
					GWT.log(caught.getMessage(), null);
				}

				public void onSuccess(List<Map> result) {	
					storePemasok.removeAll();
					storePemasok.add(ClientModule.setListBaseModelFromListMap(result));
					
					windowPltu.show();
				}
			});
		}
	 
	 private void setApprovalKit(String no_rakor, String id_pltu, final String crud){
			Map mapParam = new FastMap();
		  	mapParam.put("no_rakor", no_rakor);
		  	mapParam.put("id_pltu", id_pltu);
		  	mapParam.put("user", "Reza");
		  	mapParam.put("app_access", "WEB");
		  	mapParam.put("crud", "");
		  
		  	service.setRelationRakor("RAKORKIT", mapParam,new AsyncCallback<List<Map>>() {
		  		public void onFailure(Throwable caught) {
		  			GWT.log(caught.getMessage(), null);
		  		}

		  		public void onSuccess(List<Map> result) {	
		  			if (crud.equals("DELETE")){
		  				MessageBox.alert("Information", result.get(0).get("message").toString(), null);
		  			}
		  		}
		  	});
		}
	
}
